import Vue from 'vue'
import store from '../store'
import roles from './roles'

Vue.directive('auth', {
  inserted: function (el, binding, vnode) {
    // eslint-disable-next-line no-unused-vars
    var btnPermissionsArr = []
    if (binding.value) {
      btnPermissionsArr = binding.value
      const map = Vue.prototype.$_setMap(roles[store.getters.getUserInfo.roleCode])
      const roleNow = JSON.parse(map.get(vnode.context.$route.path))
      btnPermissionsArr.map(each => {
        const show = JSON.stringify(roleNow.permissions || '').indexOf(each) === -1
        if (show) el.parentNode && el.parentNode.removeChild(el)
      })
    }
  }
})
// 设置全局原型函数
Vue.prototype.$_setMap = function (value) {
  const setMap = new Map()
  function ergodic (params) {
    params.map(element => {
      if (element.children !== undefined) {
        ergodic(element.children)
      } else { setMap.set(element.path, JSON.stringify(element)) }
    })
  }
  ergodic(value)
  return setMap
}
